package com.aysafety.iotdataplatformweb.configurer;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @program: iotDataPlatform
 * @description: phoenix数据源配置
 * @author: ChengQi
 * @create: 2019-01-24 01:50
 **/

@Configuration
@Order(2)
@MapperScan(basePackages = PhoenixDataSource.PACKAGE, sqlSessionFactoryRef = "phoenixSqlSessionFactory")
public class PhoenixDataSource {

    static final String PACKAGE = "com.aysafety.iotdataplatformweb.dao";
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired private Environment env;

    @Bean(name = "phoenixJdbcDataSource")
    @Qualifier("phoenixJdbcDataSource")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        logger.info(env.getProperty("spring.datasource.druid.url"));
        logger.info(env.getProperty("spring.datasource.druid.driver-class-name"));
        logger.info(env.getProperty("spring.datasource.druid.username"));
        logger.info(env.getProperty("spring.datasource.druid.password"));

        dataSource.setUrl(env.getProperty("spring.datasource.druid.url"));
        dataSource.setDriverClassName(env.getProperty("spring.datasource.druid.driver-class-name"));
        dataSource.setUsername(env.getProperty("spring.datasource.druid.username"));//phoenix的用户名默认为空
        dataSource.setPassword(env.getProperty("spring.datasource.druid.password"));//phoenix的密码默认为空
        return dataSource;
    }

    @Bean(name = "phoenixJdbcTemplate")
    public JdbcTemplate phoenixJdbcTemplate(@Qualifier("phoenixJdbcDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean
    public SqlSessionFactory phoenixSqlSessionFactory(@Qualifier("phoenixJdbcDataSource") DataSource dataSource)
        throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(
            new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        sessionFactory
            .setConfigLocation(new DefaultResourceLoader().getResource("classpath:config/mybatis-config.xml"));
        return sessionFactory.getObject();
    }

    @Bean
    public DataSourceTransactionManager gasTransactionManager(
        @Qualifier("phoenixJdbcDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    public SqlSessionTemplate gasSqlSessionTemplate(
        @Qualifier("phoenixSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
